﻿using System.Data.SqlClient;
using GeneralLibs;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Lists;
using ListForms;
using Excel = Microsoft.Office.Interop.Excel;
using System.Collections;

namespace ListForms
{
    public partial class FrmDanhMucNhomVatTuHangHoa : ListForms.FrmBaseListForm
    {
        public FrmDanhMucNhomVatTuHangHoa()
        {
            InitializeComponent();
            barButtonImportFromExcel.Visibility = DevExpress.XtraBars.BarItemVisibility.Always;
        }

        private void FrmDanhMucNhomVatTuHangHoa_Load(object sender, EventArgs e)
        {
            if (DesignMode) return;
            gridDSDoiTuong.Visible = false;
            tlvDanhMucNhomVatTuHangHoa.OptionsBehavior.Editable = false;
        }
        void Instance_Change()
        {
            LoadData();
        }
        public override void LoadData()
        {
            try
            {
                tlvDanhMucNhomVatTuHangHoa.DataSource = DanhMucNhomVatTuHangHoa.Filter("LienQuanDenKho = 1").DataView;
                InitGridColumnNews();
            }
            catch
            {
            }
        }
        public void InitGridColumnNews()
        {
            foreach (DevExpress.XtraTreeList.Columns.TreeListColumn column in tlvDanhMucNhomVatTuHangHoa.Columns)
            {
                column.Visible = false;
            }

            tlvDanhMucNhomVatTuHangHoa.Columns["Ma"].Caption = "Mã";
            tlvDanhMucNhomVatTuHangHoa.Columns["Ma"].Visible = true;
            tlvDanhMucNhomVatTuHangHoa.Columns["Ten"].Caption = "Tên";
            tlvDanhMucNhomVatTuHangHoa.Columns["Ten"].Visible = true;

            tlvDanhMucNhomVatTuHangHoa.Columns["NhomVatTuHangHoaChaID"].Caption = "Phương pháp tính giá tồn kho";
            tlvDanhMucNhomVatTuHangHoa.Columns["NhomVatTuHangHoaChaID"].Visible = true;
            tlvDanhMucNhomVatTuHangHoa.Columns["NhomVatTuHangHoaChaID"].VisibleIndex = 3;

            DevExpress.XtraEditors.Controls.LookUpColumnInfo colTen = new DevExpress.XtraEditors.Controls.LookUpColumnInfo("Tên", "Tên", 20);
            DevExpress.XtraEditors.Repository.RepositoryItemLookUpEdit repTenNhomCha = new DevExpress.XtraEditors.Repository.RepositoryItemLookUpEdit();
            repTenNhomCha.Columns.Add(colTen);
            repTenNhomCha.ValueMember = "ID";
            repTenNhomCha.DisplayMember = "Ten";
            repTenNhomCha.DataSource = DanhMucPhuongPhapTinhGiaTonKho.Instance.DataView;
            DevExpress.XtraTreeList.Columns.TreeListColumn colTenNhomCha= tlvDanhMucNhomVatTuHangHoa.Columns["NhomVatTuHangHoaChaID"];
            colTenNhomCha.ColumnEdit = repTenNhomCha;
        }

        private void TlvDanhMucNhomVatTuHangHoa_DoubleClick(object sender, EventArgs e)
        {
            Edit();        
        }
        public override void CreateNew()
        {
            try
            {
                FrmEditNhomVatTuHangHoa frm = new FrmEditNhomVatTuHangHoa(null);
                frm.ShowDialog();
            }
            catch(Exception ex)
            {
            }
        }
        protected override void Edit()
        {
            try
            {
                Guid id =(Guid) tlvDanhMucNhomVatTuHangHoa.Selection[0]["ID"];
                FrmEditNhomVatTuHangHoa frm = new FrmEditNhomVatTuHangHoa(DanhMucNhomVatTuHangHoa.Instance.GetByID(id));
                frm.ShowDialog();
            }
            catch (SqlException sqlEx)
            {
                throw new CustomException(CustomException.GetSqlExceptionMessage(sqlEx));
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }
        protected override void Delete()
        {
            try
            {
                Guid id = (Guid)tlvDanhMucNhomVatTuHangHoa.Selection[0]["ID"];
                Lists.NhomVatTuHangHoa deletedObj = DanhMucNhomVatTuHangHoa.Instance.GetByProperty("ID", id);
                if (deletedObj != null)
                {
                    if (MessageBox.Show("Bạn có thực sự muốn xóa NhomVatTuHangHoa", "Xác nhận", MessageBoxButtons.YesNo) == DialogResult.No)
                        return;
                    else
                        deletedObj.DeleteFromDB();
                }
            }
            catch (SqlException sqlEx)
            {
                throw new CustomException(CustomException.GetSqlExceptionMessage(sqlEx));
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        protected override void ImportFromExcel()
        {
            try
            {
                OpenFileDialog openFileDialog = new OpenFileDialog();
                openFileDialog.Filter = "Excel file|*.xlsx;*.xls";
                if (openFileDialog.ShowDialog() == DialogResult.OK)
                {
                    string path = openFileDialog.FileName;
                    Excel.ApplicationClass app = new Microsoft.Office.Interop.Excel.ApplicationClass();
                    Excel.Workbook workBook = app.Workbooks.Open(path, 0, true, 5, "", "", true, Excel.XlPlatform.xlWindows, "\t",
                                    false, false, 0, true, 1, 0);
                    Excel.Worksheet workSheet = (Excel.Worksheet)workBook.ActiveSheet;
                    Excel.Range range = workSheet.UsedRange;

                    BasicForms.FrmProgressBarDev.StartShowProgress();
                    if (range.Columns.Count >= 3)
                    {
                        Hashtable htNhomHangKhongHople = new Hashtable();
                        int count = 1;
                        for (int rowIndex = 2; rowIndex <= range.Rows.Count; rowIndex++)
                        {
                            try
                            {
                                string maNhom = "";
                                string ten = "";
                                string p2TinhGiaTonKho = "";
                                string taikhoanKho = "";
                                string taikhoanGiaVon = "";
                                string maNhomCha = "";

                                NhomVatTuHangHoa nhomHang = null;
                                NhomVatTuHangHoa nhomCha = null;

                                try
                                {
                                    maNhom = (range.Cells[rowIndex, 2] as Excel.Range).Value2.ToString();
                                    ten = (range.Cells[rowIndex, 3] as Excel.Range).Value2.ToString();
                                    p2TinhGiaTonKho = (range.Cells[rowIndex, 4] as Excel.Range).Value2.ToString();
                                    taikhoanKho = (range.Cells[rowIndex, 5] as Excel.Range).Value2.ToString();
                                    taikhoanGiaVon = (range.Cells[rowIndex, 6] as Excel.Range).Value2.ToString();
                                    maNhomCha = (range.Cells[rowIndex, 7] as Excel.Range).Value2.ToString();

                                }
                                catch (Exception ex)
                                {
                                }

                                nhomHang = Lists.DanhMucNhomVatTuHangHoa.Instance.GetByProperty("Ma", maNhom);
                                nhomCha = Lists.DanhMucNhomVatTuHangHoa.Instance.GetByProperty("Ma", maNhomCha);

                                if (maNhom != "" && ten != "" && taikhoanKho != "" && p2TinhGiaTonKho != "" && taikhoanGiaVon != "" && nhomHang == null)
                                {
                                    nhomHang = Lists.DanhMucNhomVatTuHangHoa.Instance.NewDoiTuong();

                                    nhomHang.Ma = maNhom;
                                    nhomHang.Ten = ten;
                                    nhomHang.PhuongPhapTinhGiaTonKho = Lists.DanhMucPhuongPhapTinhGiaTonKho.Instance.GetByProperty("Ma", p2TinhGiaTonKho);
                                    nhomHang.TaiKhoanKho = Lists.DanhMucTaiKhoan.Instance.GetByProperty("SoHieu", taikhoanKho);
                                    nhomHang.TaiKhoanGiaVon = Lists.DanhMucTaiKhoan.Instance.GetByProperty("SoHieu", taikhoanGiaVon);
                                    nhomHang.NhomVatTuHangHoaCha = nhomCha;
                                    nhomHang.LienQuanDenKho = true;
                                    nhomHang.InsertToDB();
                                    BasicForms.FrmProgressBarDev.Status = "Đã xử lý " + count + " nhóm thuốc ";
                                    count++;
                                }
                                else
                                {
                                    nhomHang.Ma = maNhom;
                                    nhomHang.Ten = ten;
                                    nhomHang.PhuongPhapTinhGiaTonKho = Lists.DanhMucPhuongPhapTinhGiaTonKho.Instance.GetByProperty("Ma", p2TinhGiaTonKho);
                                    nhomHang.TaiKhoanKho = Lists.DanhMucTaiKhoan.Instance.GetByProperty("SoHieu", taikhoanKho);
                                    nhomHang.TaiKhoanGiaVon = Lists.DanhMucTaiKhoan.Instance.GetByProperty("SoHieu", taikhoanGiaVon);
                                    nhomHang.NhomVatTuHangHoaCha = nhomCha;
                                    nhomHang.LienQuanDenKho = true;
                                    nhomHang.UpdateToDB();
                                    BasicForms.FrmProgressBarDev.Status = "Đã xử lý " + count + " nhóm thuốc ";
                                    count++;
                                }
                            }
                            catch (SqlException sqlEx)
                            {
                                throw new CustomException(CustomException.GetSqlExceptionMessage(sqlEx));
                            }
                            catch (Exception ex)
                            {
                                if (ex is CustomException) throw ex;
                                else
                                {
                                    throw new Exception(ex.Message);
                                }
                            }
                        }
                    }
                    workBook.Close(false, path, true);
                }
            }
            catch (Exception ex)
            {
                BasicForms.FrmExceptionNotifier.ShowAndLog(ex);
            }
            BasicForms.FrmProgressBarDev.FinishShowProgress();
        }

        private void FrmDanhMucNhomVatTuHangHoa_ExportExcel()
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.Title = "Chọn tên file excel để xuất (*.xls)...";
            saveFileDialog.Filter = "Excel file|*.xls";
            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                BasicForms.FrmProgressBarDev.StartShowProgress();
                tlvDanhMucNhomVatTuHangHoa.ExportToXls(saveFileDialog.FileName);
            }
        }
    }
}
